package com.yc.ycbook.common.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.yc.yclibx.adapter.YcPageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class CommonPageAdapter extends YcPageAdapter {
    private List<Fragment> mFragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private boolean isChangeType;

    public CommonPageAdapter(FragmentManager fm) {
        super(fm);
    }

    public void add(Fragment fragment) {
        add(fragment, "");
    }

    public void add(Fragment fragment, String titles) {
        mFragments.add(fragment);
        mTitles.add(titles);
        isChangeType = true;
    }

    public void addAll(List<Fragment> fragments, List<String> titles) {
        addAll(fragments, titles, false);
    }

    public void addAll(List<Fragment> fragments, List<String> titles, boolean isClear) {
        if (isClear) {
            mFragments.clear();
            mTitles.clear();
        }
        mFragments.addAll(fragments);
        mTitles.addAll(titles);
        isChangeType = true;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        isChangeType = false;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }

    @Override
    protected boolean isChangeType() {
        return isChangeType;
    }

    @Override
    protected int getFragmentIndex(Fragment fragment) {
        return mFragments.indexOf(fragment);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }
}
